package com.hpx.javaeye.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import net.rim.device.api.io.Base64OutputStream;
import net.rim.device.api.ui.component.Dialog;

public abstract class Authentication implements AuthenticationCallBack {
	private BlackberryLogger logger = BlackberryLogger.getInstance();

	public void passHttpBasic(String url, String username, String password) {

		HttpConnection c = null;

		InputStream is = null;
		OutputStream os = null;
		int rc;

		String encryptStr = username + ":" + password;
		try {
			encryptStr = Base64OutputStream.encodeAsString(encryptStr
					.getBytes(), 0, encryptStr.length(), false, false);
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		String base64_credentials = "Basic " + encryptStr;
		System.out.println("Base64:" + base64_credentials);
		try {
			System.out.println("usl is : " + url);
			c = (HttpConnection) Connector.open(url);
			System.out.println("connection is open......hpx debug.");
			// Set the request method and headers
			c.setRequestMethod(HttpConnection.GET);
			c.setRequestProperty("If-Modified-Since",
					"29 Oct 1999 19:43:31 GMT");
			c.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
			c.setRequestProperty("Accept-Encoding", "gzip");
			c.setRequestProperty("User-Agent", "MyUserAgentHere");
			c.setRequestProperty("Content-Language", "en-US");
			c.setRequestProperty("Cache-Control",
					"no-store, no-cache, must-revalidate");
			c.setRequestProperty("Authorization", base64_credentials);

			// Getting the output stream may flush the headers
			os = c.openOutputStream();
			System.out.println("output is open");
			// Getting the response code will open the connection,
			// send the request, and read the HTTP response headers.
			// The headers are stored until requested.
			rc = c.getResponseCode();

			System.out.println("i got the response code rc is :" + rc);

			if (rc != HttpConnection.HTTP_OK) {
				throw new IOException("HTTP response code: " + rc);
			}

			is = c.openInputStream();
			System.out.println("input is open .");
			// Get the ContentType
			String type = c.getType();
			processType(type);

			// Get the length and process the data
			int len = (int) c.getLength();

			System.out.println("len is : " + len);

			if (len > 0) {
				int actual = 0;
				int bytesread = 0;
				byte[] data = new byte[len];
				while ((bytesread != len) && (actual != -1)) {
					actual = is.read(data, bytesread, len - bytesread);
					bytesread += actual;
				}
				process(data);
			} else {
				int ch;
				while ((ch = is.read()) != -1) {
					process((byte) ch);
				}
			}

		} catch (Exception e) {
			// Dialog.alert(e.getMessage());
			System.out.println(e.getMessage());
			System.exit(0);

		}
	}

	public void postRequest(String url) throws IOException {
		HttpConnection c = null;

		InputStream is = null;
		OutputStream os = null;
		int rc;
		c = (HttpConnection) Connector.open(url);
		System.out.println("connection is open......hpx debug.");
		// Set the request method and headers
		c.setRequestMethod(HttpConnection.GET);
		c.setRequestProperty("If-Modified-Since",
				"29 Oct 1999 19:43:31 GMT");
		c.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
		c.setRequestProperty("Accept-Encoding", "gzip");
		c.setRequestProperty("User-Agent", "MyUserAgentHere");
		c.setRequestProperty("Content-Language", "en-US");
		c.setRequestProperty("Cache-Control",
				"no-store, no-cache, must-revalidate");
		//c.setRequestProperty("Authorization", base64_credentials);

		// Getting the output stream may flush the headers
		os = c.openOutputStream();
		System.out.println("output is open");
		// Getting the response code will open the connection,
		// send the request, and read the HTTP response headers.
		// The headers are stored until requested.
		rc = c.getResponseCode();

		System.out.println("i got the response code rc is :" + rc);

		if (rc != HttpConnection.HTTP_OK) {
			throw new IOException("HTTP response code: " + rc);
		}

		is = c.openInputStream();
		System.out.println("input is open .");
		// Get the ContentType
		String type = c.getType();
		processType(type);

		// Get the length and process the data
		int len = (int) c.getLength();

		System.out.println("len is : " + len);

		if (len > 0) {
			int actual = 0;
			int bytesread = 0;
			byte[] data = new byte[len];
			while ((bytesread != len) && (actual != -1)) {
				actual = is.read(data, bytesread, len - bytesread);
				bytesread += actual;
			}
			process(data);
		} else {
			int ch;
			while ((ch = is.read()) != -1) {
				process((byte) ch);
			}
		}
	}
}
